#include "tictactoe.h"
#include "button.h"
#include <QGridLayout>#include <QGridLayout>
#include <QPainter>
#include <QDebug>
#include <QMessageBox>

TicTacToe::TicTacToe(QWidget * parent) :
        QDialog(parent),
        my_turn(Button::cross),
        my_turns_counter(0)
{
    QGridLayout * grid = new QGridLayout();

    setLayout(grid);
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            my_bttns[3*i + j] = new Button(this);
            connect(my_bttns[3*i + j], SIGNAL(buttonclick(Button*)),
                    this, SLOT(buttonclicked(Button*)));
            connect(my_bttns[3*i + j], SIGNAL(stateChanged()),
                    this, SLOT(buttonStateChanged()));
            grid -> addWidget(my_bttns[3*i + j], i, j);
        }
    }
}


void TicTacToe::buttonclicked(Button *button)
{
    if(button->getDrawItem() == Button::none)
    {
        button->setDrawItem(my_turn);
        my_turn = (my_turn == Button::cross
                   ? Button::toe : Button::cross);
        my_turns_counter++;
    }
    else
    {
        QMessageBox::information(
                this,
                "Wrong turn!",
                "This buttorn has been already pushed!");
    }
}

void TicTacToe::buttonStateChanged()
{
    checkWin();
}

void TicTacToe::checkWin()
{
    QString messange_text = QString::null;
    bool game_end = false;
    for(int i = 0; i < 3; i++)
    {
        if(my_bttns[i]->getDrawItem() == my_bttns[i + 3]->getDrawItem()
           && my_bttns[i + 3]->getDrawItem() == my_bttns[i + 6]->getDrawItem()
            && my_bttns[i]->getDrawItem() != Button::none
            && my_bttns[i+3]->getDrawItem() != Button::none
            && my_bttns[i+6]->getDrawItem() != Button::none)
        {
            messange_text = (my_bttns[i]->getDrawItem() == Button::cross
                           ? "Cross player won!" : "Toe player won!");
            game_end = true;
        }
        else if(my_bttns[i*3]->getDrawItem() == my_bttns[i*3 + 1]->getDrawItem()
           && my_bttns[i*3 + 1]->getDrawItem() == my_bttns[i*3 + 2]->getDrawItem()
            && my_bttns[i*3]->getDrawItem() != Button::none
            && my_bttns[i*3+1]->getDrawItem() != Button::none
            && my_bttns[i*3+2]->getDrawItem() != Button::none)
        {
            messange_text = (my_bttns[i*3]->getDrawItem() == Button::cross
                           ? "Cross player won!" : "Toe player won!");
            game_end = true;
        }
    }
    if(my_bttns[0]->getDrawItem() == my_bttns[4]->getDrawItem()
       && my_bttns[4]->getDrawItem() == my_bttns[8]->getDrawItem()
        && my_bttns[0]->getDrawItem() != Button::none
        && my_bttns[4]->getDrawItem() != Button::none
        && my_bttns[8]->getDrawItem() != Button::none)
    {
        messange_text = (my_bttns[4]->getDrawItem() == Button::cross
                       ? "Cross player won!" : "Toe player won!");
        game_end = true;
    }
    else if(my_bttns[2]->getDrawItem() == my_bttns[4]->getDrawItem()
       && my_bttns[4]->getDrawItem() == my_bttns[6]->getDrawItem()
        && my_bttns[2]->getDrawItem() != Button::none
        && my_bttns[4]->getDrawItem() != Button::none
        && my_bttns[6]->getDrawItem() != Button::none)
    {
        messange_text = (my_bttns[4]->getDrawItem() == Button::cross
                       ? "Cross player won!" : "Toe player won!");
        game_end = true;
    }

    if((game_end == true && messange_text != QString::null) | my_turns_counter == 9)
    {
        QMessageBox msgBox;
        msgBox.setText("Game over");
        msgBox.setText(game_end ? messange_text : "Friendship won!");
        msgBox.setInformativeText("Do you want to play more?");
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msgBox.setDefaultButton(QMessageBox::Yes);
        int ret = msgBox.exec();

        switch(ret)
        {
            case QMessageBox::Yes:
                this->newGame();
                break;
            case QMessageBox::No:
                this->close();
                break;
            default:
                this->close();
                break;
        }
    }

}

void TicTacToe::newGame()
{
    for(int i = 0; i < 9; i++)
    {
        my_bttns[i]->setDrawItem(Button::none);
    }
    my_turn = Button::cross;
    my_turns_counter = 0;
}
